package com.hubei.car.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hubei.car.dto.req.PageCondition;
import com.hubei.car.entity.CarClassify;
import com.hubei.car.entity.PageResult;
import com.hubei.car.handler.BaseException;
import com.hubei.car.mapper.CarClassifyMapper;
import com.hubei.car.service.CarClassifyService;
import com.hubei.car.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author luokangtao
 * @create 2019-08-30 12:36
 */
@Service
@Slf4j
public class CarClassifyServiceImpl implements CarClassifyService {

    @Autowired
    CarClassifyMapper carClassifyMapper;

    @Override
    @Transactional
    public String add(CarClassify carClassify) {
        QueryWrapper<CarClassify> querywrapper = new QueryWrapper<>();
        querywrapper.eq("sort", carClassify.getSort());//排序值
        querywrapper.eq("classifytype",carClassify.getClassifytype());//分类类型
        if(carClassify.getClassifytype()==1){
            carClassify.setRelationId(0L);
        }
        querywrapper.eq("relation_id",carClassify.getRelationId());//关联id
        //判断当前排序值是否存在
        CarClassify one = carClassifyMapper.selectOne(querywrapper);
        if (one != null) {
            //如果存在则大于等于他的全部+1
            Integer integer = carClassifyMapper.addAllSortOne(carClassify);
            log.info("添加的时候修改sort:[{}]", integer);
        }
        //创建时间
        carClassify.setCreateTime(LocalDateTime.now());
        int add = carClassifyMapper.insert(carClassify);
        if (add > 0) {
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    @Transactional
    public String update(CarClassify carClassify) {
        if(carClassify.getClassifytype()==1){
            carClassify.setRelationId(0L);
        }
        //判断当前排序值是否存在
        Integer one = carClassifyMapper.selectByOne(carClassify);
        if (one != null) {
            //如果存在则大于等于他的全部+1
            Integer integer = carClassifyMapper.updateAllSortOne(carClassify);
            log.info("添加的时候修改sort:[{}]", integer);
        }
        //修改时间
        carClassify.setUpdateTime(LocalDateTime.now());
        int update = carClassifyMapper.updateById(carClassify);
        if (update > 0) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    @Transactional
    public String deleteOne(Long id) {
        //查询当前id有没关联关系
        QueryWrapper<CarClassify> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("relation_id",id);
        queryWrapper.eq("classifytype",2);
        Integer integer = carClassifyMapper.selectCount(queryWrapper);
        if(integer>0){
            throw new BaseException("当前一级分类有所属下级,所以不能删除");
        }
        int delete = carClassifyMapper.deleteById(id);
        if (delete > 0) {
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public CarClassify queryOne(Long id) {
        return carClassifyMapper.selectById(id);
    }

    @Override
    public PageResult queryAll(PageCondition pageCondition, int classifytype) {
        //分页
        Page<CarClassify> page=new Page<>(pageCondition.getCurrent(),pageCondition.getSize());
        if(classifytype==1){
            //排序降序
            page.setAsc("sort");
        }else{
            List<String> list = new ArrayList<>();
            list.add("relation_id");
            list.add("sort");
            page.setAscs(list);
        }
        //查询条件
        QueryWrapper<CarClassify> querywrapper=new QueryWrapper<>();
        if(StrUtil.isNotBlank(pageCondition.getKeywordOne())){
            //分类名称
            querywrapper.like("classifyname",pageCondition.getKeywordOne());
        }
        if(StrUtil.isNotBlank(pageCondition.getKeywordTwo())){
            //关联id
            querywrapper.eq("relation_id",pageCondition.getKeywordTwo());
        }
        querywrapper.eq("classifytype",classifytype);
        IPage<CarClassify> imgIPage = carClassifyMapper.selectPage(page, querywrapper);
        for (CarClassify record : imgIPage.getRecords()) {
            //时间转换格式
            record.setCreateTimeString(DateUtil.localDateTimeToString(record.getCreateTime()));
            if(record.getUpdateTime()!=null){
                record.setUpdateTimeString(DateUtil.localDateTimeToString(record.getUpdateTime()));
            }
            //如果是二级分类 查询关联的一级分类名称
            if(record.getRelationId()>0){
                CarClassify hbClassify = carClassifyMapper.selectById(record.getRelationId());
                record.setRelationNameString(hbClassify.getClassifyname());
            }
        }
        return new PageResult(pageCondition.getCurrent(),pageCondition.getCurrent(),imgIPage.getTotal(),imgIPage.getRecords());
    }


}
